<?php
namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;
require_once ('Common.php');

/**
 * 微信验证组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Validate extends Common
{
    
	function __construct($appid,$secret,$token)
	{
        parent::__construct($appid,$secret,$token);
	}

	function __destruct()
	{
        
	}
    /**
     * 验证来自微信服务器
     * @param type $params
     * @param type $token
     * @return boolean
     */
    public function checkSignature($params)
    {
        $signature = isset($params["signature"]) ? $params["signature"] : "";
        $timestamp = isset($params["timestamp"]) ? $params["timestamp"] : "";
        $nonce = isset($params["nonce"]) ? $params["nonce"]: "";
        $tmpArr = array($this->token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = sha1(implode( $tmpArr ));
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
	public function getAccessToken()
	{
       $result = Tools::httpGet(self::API_URL_PREFIX . self::AUTH_URL.'appid=' . $this->appid . '&secret=' . $this->secret);
       $result = $this->parsing($result);
       return $result;
	}
    public function setAccessToken($access_token='')
    {
        if(empty($access_token))
        {
            $this->getAccessToken();
        }
        else
        {
            $this->access_token = $access_token;
        }
        
        return $this->access_token;
    }

}
?>